package cn.geapi.core.filter.aop;

import java.lang.reflect.Method;

import org.apache.log4j.Logger;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

public class LoggerAOP implements MethodBeforeAdvice, AfterReturningAdvice {
	private static final Logger LOGGER = Logger.getLogger(LoggerAOP.class);
	private StringBuffer buffer;

	public void afterReturning(Object returnValue, Method method,
			Object[] args, Object target) throws Throwable {
		buffer = new StringBuffer();
		buffer.append("execute ").append(target.getClass().getName())
				.append(".").append(method.getName())
				.append(" after return,return value is ").append(returnValue);
		LOGGER.info(buffer.toString());
	}

	public void before(Method method, Object[] args, Object target)
			throws Throwable {
		buffer = new StringBuffer();
		buffer.append("execute ").append(target.getClass().getName())
				.append(".").append(method.getName()).append(" args is ");
		if (null == args) {
			buffer.append("null");
		} else {
			for (Object arg : args) {
				buffer.append(arg.toString()).append(",");
			}
			buffer.setLength(buffer.length() - 1);
		}
		LOGGER.info(buffer.toString());

	}

}
